package com.wlient.springboot_stomp.service;

import com.wlient.springboot_stomp.pojo.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author: 史炜立
 * @description: TODO
 * @date: 2022/10/31 16:59
 */
@Service
public class TestService {

    @Autowired
    public SimpMessagingTemplate template;

    public AtomicInteger num = new AtomicInteger(1);

    @Scheduled(fixedRate = 10000)
    public void sendMessge() {
//        System.out.println("后台广播推送！" + num.getAndIncrement());
        Message msg = new Message();
        msg.setCode("200");
        msg.setContent("后台广播推送！" + num.getAndIncrement());
        msg.setForm("wlient");
        msg.setTo("all");
        template.convertAndSend("/topic/all", msg.getContent());
    }


    //一对一推送消息
    @Scheduled(cron = "*/5 * * * * ?")
    public void sendQueueMessage() {
        System.out.println("后台一对一推送！");
        Message msg = new Message();
        msg.setCode("200");
        msg.setContent("后台一对一推送！" );
        msg.setForm("wlient");
        msg.setTo("swl");
        this.template.convertAndSendToUser(msg.getTo() + "", "/queue/getResponse", msg);
    }
}
